const Controller = require('egg').Controller;
class HomeController extends Controller {
  async index() {
    const { ctx } = this;
    ctx.body = 'hi, egg';
  }
  async login() {
    const { ctx ,service} = this
    const {username,password} = ctx.request.body
    const results = await service.home.isLogin({
      username,
      password
    })
    if(results.isExists){
      this.ctx.body = {
        code:1,
        msg:"登录成功"
      }
    }else{
      this.ctx.body = {
        code:0,
        msg:"登录失败"
      }
    }
  }
  //注册 
  async register() {
    const { ctx,service} = this
    const {username,password,role} = ctx.request.body
    console.log(role)
    const results = await service.home.checkUser({
      username
    })
    if(results.code === 0){
      this.ctx.body = results
    }else{
      if(results.isExists){
        this.ctx.body = {
          code: 10001,
          msg: '此用户名已被占用'
        }
      }else{
        const register = await service.home.reigster({username,password,role})
        ctx.body = register
      }
    }
  } 
  //动态路由
  async info() {
    const { ctx } = this;
    ctx.body = {
      name: `hello ${ctx.params.id}`,
    };
  }
}

module.exports = HomeController;
